#!/usr/bin/env python
from tzutil.extract import extract
from tzutil.csv_li import csv_li
from tzutil.retry import retry
import re
import sys
from time import time
import datetime
import requests

# 上证综指代码：000001.ss，深证成指代码：399001.SZ，沪深300代码：000300.ss； 香港为 0001.hk;加拿大股指代码：cnu.to;新西兰股指代码为.nz


def get_page_data(symbol):
    url = "https://hk.finance.yahoo.com/quote/%s/?p=%s" % (symbol, symbol)
    r = requests.get(url, timeout=60)
    cookie = {'B': r.cookies['B']}
    # lines = r.text.encode('utf-8').strip().replace('}', '\n')
    lines = r.content.decode('utf-8')
    return cookie, lines


def get_cookie_crumb(symbol):
    cookie, lines = get_page_data(symbol)
    crumb = extract('"CrumbStore":{"crumb":"', '"},', lines)

    # Note: possible \u002F value
    # ,"CrumbStore":{"crumb":"FWP\u002F5EFll3U"
    # FWP\u002F5EFll3U
    crumb2 = crumb.encode('utf-8').decode('unicode_escape')
    return cookie, crumb2


def get_data(symbol, begin, end_date, cookie, crumb):
    url = "https://query1.finance.yahoo.com/v7/finance/download/%s?period1=%s&period2=%s&interval=1d&events=history&crumb=%s" % (
        symbol, begin, end_date, crumb
    )
    response = requests.get(url, cookies=cookie, timeout=60)
    return response.content.decode('utf-8')


from tzutil.day import str2day


def csv_verify(row):
    # day, open, high, low, close, _close, volume
    try:
        volume = int(row[-1])
    except ValueError:
        return
    row[0] = str2day(row[0])
    for i in range(1, 6):
        row[i] = float(row[i])

    row[-1] = volume
    return row


class YahooPrice:

    def __init__(self):
        self._cookie = None

    @retry
    def __call__(self, symbol, begin=0):
        if self._cookie is None:
            self._cookie = get_cookie_crumb('AAPL')
        end_date = int(time())
        cookie, crumb = self._cookie
        csv = get_data(symbol, begin, end_date, cookie, crumb)
        csv = csv_li(csv, csv_verify)
        return csv


if __name__ == '__main__':
    # ,'0656.HK']
    price = YahooPrice()
    print(price("0656.HK"))
